﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FlowerShop.Models;
namespace FlowerShop.DataAccessLayer
{
    public class OrdersDAO
    {
        // Xử lí ở bảng Order
        private static FlowerDBEntities  db = new FlowerDBEntities();
        public static bool InsertOrders(OrderModels order)
        {
            Order od = Mapper.orderFromModel(order);
            try
            {
                db.AddToOrders(od);
                db.SaveChanges();
                return true;
            }
            catch (NullReferenceException)
            {
                return false;
            }
        }
        public static bool UpdateOrders(OrderModels order)
        {
            try
            {
                var od = db.Orders.Single(o => o.OrderID == order.OrderID);
                Order odr = Mapper.orderFromModel(order);
                od.CustomerID = odr.CustomerID;
                od.OrderDate = odr.OrderDate;
                od.RequireDate = odr.RequireDate;
                od.ShipAddress = odr.ShipAddress;
                od.ShipName = odr.ShipName;
                od.Phone = odr.Phone;
                od.Mobile = odr.Mobile;
                od.Messages = odr.Messages;
                od.Desription = odr.Desription;
                od.PaidTypeID = odr.PaidTypeID;
                od.PriceSum = odr.PriceSum;
                od.Status = odr.Status;
                db.SaveChanges();
                return true;
            }
            catch (NullReferenceException)
            {
                return false;
            }
        }
        public static bool DeleteOrders(int id)
        {
            try
            {
                Order od = db.Orders.Single(o => o.OrderID == id);
                db.Orders.DeleteObject(od);
                db.SaveChanges();
                return true;
            }
            catch (NullReferenceException)
            {
                return false;
            }
        }
        public static List<Order> SearchOrders(string str)
        {
            List<Order> _Order = (from o in db.Orders where o.ShipName.ToLower().Contains(str.ToLower()) select o).ToList();
            return _Order;
        }
        public static Order SearchOrders(int id)
        {
            var od = db.Orders.Single(o => o.OrderID == id);
            return od;
        }
        public static List<Order> GetAllOrders()
        {
            List<Order> Od = (from o in db.Orders select o).ToList();
            return Od;
        }
    }
}